import csv

import requests
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np


class HDD:
    def __init__(self, Name, ID, PID, Platform, Price, Capacity, Slot, CacheSize, Platter, RPM, ReadWriteSpeed,
                 ReadWriteSpeed4K):
        self.Name = Name
        self.ID = ID
        self.PID = PID
        self.Platform = Platform
        self.Price = Price
        self.Capacity = Capacity
        self.Slot = Slot
        self.CacheSize = CacheSize
        self.Platter = Platter
        self.RPM = RPM
        self.ReadWriteSpeed = ReadWriteSpeed
        self.ReadWriteSpeed4K = ReadWriteSpeed4K

    def tranTo(self, begin, after):
        if begin == '适用类型':
            self.Platform = after
        elif begin == '硬盘容量':
            self.Capacity = after
        elif begin == '接口类型':
            self.Slot = after
        elif begin == '缓存':
            self.CacheSize = after
        elif begin == '盘片数量':
            self.Platter = after
        elif begin == '转速':
            self.RPM = after
        elif begin == '读写速率':
            self.ReadWriteSpeed = after
        elif begin == '4k读写速率':
            self.ReadWriteSpeed4K = after


def get_html(url):
    # 模拟浏览器访问
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54',
        'accept-language': 'zh-CN,zh;q=0.9'
    }
    print("--> 正在获取网站信息")
    response = requests.get(url, headers=headers)  # 请求访问网站
    if response.status_code == 200:
        html = response.text  # 获取网页源码
        return html  # 返回网页源码
    else:
        print("获取网站信息失败！")


if __name__ == '__main__':
    csv_file = open('HDDInfo.csv', 'w', newline='', encoding='utf-8')
    writer = csv.writer(csv_file)
    writer.writerow(
        ['Name', 'ID', 'PID', 'Platform', 'Price', 'Capacity', 'Slot', 'CacheSize', 'Platter', 'RPM', 'ReadWriteSpeed',
         'ReadWriteSpeed4K'])

    data = pd.read_csv("../IDs/HDD.csv")
    col_1 = data["ID"]
    col_2 = data['PicPath']
    col_3 = data['Name']
    col_4 = data['Price']
    ids = np.array(col_1)
    picPath = np.array(col_2)
    names = np.array(col_3)
    prices = np.array(col_4)
    for i in range(len(ids)):
        # for i in range(100):
        search_url = 'https://detail.zol.com.cn/1428/' + str(ids[i]) + '/param.shtml'
        html = get_html(search_url)
        soup = BeautifulSoup(html, 'lxml')
        beginList = soup.find_all('th')
        afterList = soup.find_all('td', class_='hover-edit-param')
        case = HDD('null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null')
        case.Name = names[i]
        case.ID = ids[i]
        case.PID = picPath[i]
        case.Price = prices[i]
        for j in range(len(beginList)):
            begin = beginList[j].span.get_text()
            after = afterList[j].span.get_text()
            case.tranTo(begin, after)
        writer.writerow(
            [case.Name, case.ID, case.PID, case.Platform, case.Price, case.Capacity, case.Slot, case.CacheSize,
             case.Platter, case.RPM, case.ReadWriteSpeed, case.ReadWriteSpeed4K])
        i += 1
    csv_file.close()
